package com.syj.collect.Present;

import android.util.Log;

import com.google.gson.Gson;
import com.syj.collect.base.BasePresent;
import com.syj.collect.entry.TaskBean;
import com.syj.collect.entry.UserInfoBean;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.view.HomeView;
import com.syj.collect.view.TaskActivityView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class TaskActivityPresent extends BasePresent<TaskActivityView> {

    public void getTaskList() {

        BaseRequest.getInstance().getApiServise().getUserTask(2, SPUtil.getPrefString("token", ""))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
                        Gson gson = new Gson();
                        TaskBean gBean = gson.fromJson(value, TaskBean.class);

//                        Log.e("结果?", value);
                        if (gBean.getCode() == 0) {
                            view.initSuccess( gBean.getData().getCurrPage(),gBean.getData().getList());
                        } else {
                            view.initFail(gBean.getMsg());
                        }

                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    public void getTaskList1(int type) {
        Gson gson = new Gson();
        UserInfoBean.UserInfo gsb = gson.fromJson(SPUtil.getPrefString("userInfo", ""), UserInfoBean.UserInfo.class);

        HashMap hashMap = new HashMap();
        hashMap.put("status", "1");//1 未采集  2采集中  3已采集
        hashMap.put("userId", gsb.getUserId());
        hashMap.put("grade", ""+type);//1常规  2紧急
        //hashMap.put("endTime", "");
        //hashMap.put("startTime", "");


//        String md5string=StringUtils.initString(hashMap);
//        hashMap.put("sign",md5string);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), gson.toJson(hashMap));
        BaseRequest.getInstance().getApiServise().getUserTask1(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
                        Gson gson = new Gson();
                        TaskBean gBean = gson.fromJson(value, TaskBean.class);

//                        Log.e("结果?", value);
                        if (gBean.getCode() == 0) {
                            view.initSuccess(gBean.getData().getCurrPage(),gBean.getData().getList());
                        } else {
                            view.initFail(gBean.getMsg());
                        }

                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }
}
